/*
 * @Author: wjc
 * @Date: 2025-09-28 09:02:43
 * @LastEditors: wjc
 * @LastEditTime: 2025-10-28 10:26:14
 * @Description:
 */
import { api } from '#/api'

export type ScaffoldEditor = {
  data?: any
  get: (() => string) | string
  id: number | string
  update: (() => string) | string
  visible: boolean
}

class Scaffold {
  create(config: any): void {
    config.value.model = {}
    config.value.visible = true
  }

  creator(
    entity: string,
    reset = {
      get: undefined,
      store: undefined,
    }
  ) {
    return {
      visible: false,
      model: {},
      get: reset.get ?? api[entity].create,
      store: reset.store ?? api[entity].store,
    }
  }

  edit(config: any, id: number | string, index = 0): void {
    config.value.id = id
    config.value.index = index
    config.value.visible = true
  }

  editor(
    entity: string,
    reset = {
      get: undefined,
      update: undefined,
    }
  ): ScaffoldEditor {
    return {
      visible: false,
      id: 0,
      get: reset.get ?? api[entity].edit,
      update: reset.update ?? api[entity].update,
    }
  }

  pager() {
    return {
      page: 1,
      page_limit: 15,
      total: 0,
    }
  }

  search(data: object, searchForm: any, pageForm: any, fun: () => void): void {
    searchForm.value.data = data
    pageForm.value.page = 1
    fun()
  }

  searcher() {
    return {
      data: {},
      fields: [],
    }
  }

  show(config: any, id: number | string): void {
    config.value.id = id
    config.value.visible = true
  }

  shower(
    entity: string,
    reset = {
      show: undefined,
    }
  ) {
    return {
      id: 0,
      visible: false,
      get: reset.show ?? api[entity].show,
    }
  }
}

const scaffold = new Scaffold()
export default scaffold
